home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Text / print / HPDJ870Src.lha / render.c < prev    next >
C/C++ Source or Header  |  2004-05-16  |  12KB  |  330 lines

  1. /*
  2.  *        Render function
  3.  *
  4.  *        HP_DeskJet_870C driver with color gfx
  5.  */
  6.  
  7. #include "global.h"
  8.  
  9. #define NUMSTARTCMD     7       /* # of cmd bytes before binary data */
  10. #define NUMENDCMD       0       /* # of cmd bytes after binary data */
  11. #define NUMTOTALCMD (NUMSTARTCMD + NUMENDCMD)   /* total of above */
  12. #define LENGTH          8
  13. #define PLANES         27
  14. #define WIDTH          33
  15. #define NUMCOLORCMD     7       /* Cmds for each color buffer */
  16. #define MAXCOLORBUFS    4
  17. #define STARTCMDSIZE   48
  18.  
  19. /*
  20.         00-04   \033&l0L        perf skip mode off 
  21.         05-11   \033&l000F      set page length (LENGTH)
  22.         12-18   \033*t075R      set raster graphics resolution (dpi=15)
  23.         19-23   \033*b0M        add compression (Method 0)
  24.         24-29   \033*r01U       Num of raster planes per row (PLANES)
  25.         30-37   \033*r1440S     set raster gfx width (WIDTH)
  26.         38-42   \033*o3D        Set Gfx depletion (mid value)
  27.         43-47   \033*r0A        start raster graphics
  28. */
  29. char StartCmd[STARTCMDSIZE+1] = "\033&l0L\033&l000F\033*t075R\033*b0M\033*r01U\033*r1440S\033*o3D\033*r0A";
  30. static UWORD NumColorBufs, RowSize;
  31. STATIC UBYTE *gamma_table;
  32.  
  33. extern struct PrinterData *PD;
  34. extern struct PrinterExtendedData *PED;
  35. extern UBYTE GammaTables[15][256];
  36.  
  37. /* Render graphics to printer */
  38.  
  39. int Render(long ct, long x, long y, long status)
  40. {      
  41.         static UWORD textlength;
  42.         static ULONG BufSize;
  43.         static UBYTE *colors[MAXCOLORBUFS];
  44.         UBYTE *ptr;
  45.         int i, err, papersize, lpi, len;
  46.         long row;
  47.  
  48.         err=PDERR_NOERR;
  49.         switch(status) {
  50.                 case 0 : /* Master Initialization */
  51.                         /*
  52.                                 ct      - pointer to IODRPReq structure.
  53.                                 x       - width of printed picture in pixels.
  54.                                 y       - height of printed picture in pixels.
  55.                         */
  56.                         RowSize = (x + 7) / 8;
  57.                         if (PD->pd_Preferences.PrintShade == SHADE_COLOR) {
  58.                            NumColorBufs = MAXCOLORBUFS;
  59.                            StartCmd[PLANES] = '-'; /* 4 planes, KCMY */
  60.                            StartCmd[PLANES+1] = '4';
  61.                         }
  62.                         else
  63.                         {
  64.                            NumColorBufs = 1;
  65.                         }
  66.                         BufSize = RowSize * 2; /* Double buffered */
  67.                         
  68.                         lpi = 6;
  69.                         if (PD->pd_Preferences.PrintSpacing == EIGHT_LPI)
  70.                            lpi = 8;
  71.                            
  72.                         papersize = PD->pd_Preferences.PaperSize;
  73.                 switch (papersize) 
  74.                 {
  75.                 case US_LETTER:
  76.                               textlength = 10 * lpi;
  77.                     break;
  78.                 case US_LEGAL:
  79.                               textlength = 13 * lpi;                        
  80.                                 break;
  81.                 case EURO_A4:
  82.                                textlength = (lpi ==8 ? 85 : 65);                        
  83.                     break;
  84.                 default:   /* Default size */
  85.                               textlength = PD->pd_Preferences.PaperLength;            
  86.                 }
  87.                         
  88.                         /* Set page length */
  89.                         StartCmd[LENGTH] = textlength / 100 | '0';
  90.                         textlength = textlength % 100;
  91.                         StartCmd[LENGTH+1] = textlength / 10 | '0';
  92.                         StartCmd[LENGTH+2] = textlength % 10 | '0';
  93.                         
  94.                         row = x;           /* Set Raster Width */
  95.                         StartCmd[WIDTH] = row / 1000 | '0';
  96.                         row = row % 1000;
  97.                         StartCmd[WIDTH+1] = row / 100 | '0';
  98.                         row = row % 100;
  99.                         StartCmd[WIDTH+2] = row / 10 | '0';
  100.                         StartCmd[WIDTH+3] = row % 10 | '0';
  101.  
  102.                         if (PD->pd_Preferences.PrintThreshold>0)
  103.                            gamma_table = GammaTables[PD->pd_Preferences.PrintThreshold];
  104.                         else
  105.                            gamma_table = NULL;
  106.                            
  107.                                                
  108.                         for (i=0; i< NumColorBufs; i++) {
  109.                             if (!(colors[i] = AllocMem(BufSize, MEMF_PUBLIC)))
  110.                                err = PDERR_BUFFERMEMORY;
  111.                         }
  112.  
  113.                         if (err != PDERR_BUFFERMEMORY) { 
  114.                              /* perf skip mode off, set dpi, start raster gfx */
  115.                             err = (*(PD->pd_PWrite))(StartCmd, STARTCMDSIZE-1);
  116.  
  117.                         }
  118.                         break;
  119.  
  120.                 case 1 : /* Scale, Dither and Render */
  121.                         /*
  122.                                 ct      - pointer to PrtInfo structure.
  123.                                 x       - 0.
  124.                                 y       - row # (0 to Height - 1).
  125.                         */
  126.                         Transfer((struct PrtInfo *)ct, y, colors, RowSize, NumColorBufs);
  127.                         err = PDERR_NOERR; /* all ok */
  128.                         break;
  129.  
  130.                 case 2 : /* Dump Buffer to Printer */
  131.                         /*
  132.                                 ct      - 0.
  133.                                 x       - 0.
  134.                                 y       - # of rows sent (1 to NumRows).
  135.  
  136.                                 White-space strip.
  137.                         */
  138.                        if (gamma_table != NULL && NumColorBufs == 4) 
  139.                           CorrectColours(gamma_table,colors, BufSize);
  140.                        
  141.                        if (NumColorBufs == 4)  
  142.                           err = DumpColorPrint(colors, BufSize);
  143.                        else
  144.                           err = DumpBWPrint(colors,BufSize);                          
  145.                        break;
  146.  
  147.                 case 3 : /* Clear and Init Buffer */
  148.                         /*
  149.                                 ct      - 0.
  150.                                 x       - 0.
  151.                                 y       - 0.
  152.                         */
  153.                         for (i=0; i<NumColorBufs; i++) {
  154.                             ptr = colors[i];
  155.                             len = BufSize;
  156.                              do {
  157.                                 *ptr++ = 0;
  158.                              } while (--len);
  159.                         }
  160.                         break;
  161.  
  162.                 case 4 : /* Close Down */
  163.                         /*
  164.                                 ct      - error code.
  165.                                 x       - io_Special flag from IODRPReq struct
  166.                                 y       - 0.
  167.                         */
  168.                         err = PDERR_NOERR; /* assume all ok */
  169.                         /* if user did not cancel the print */
  170.                         if (ct != PDERR_CANCEL) {
  171.                                 /* end raster graphics, perf skip mode on */
  172.                                 if ((err = (*(PD->pd_PWrite))
  173.                                         ("\033*rbC\033&l1L", 10)) == PDERR_NOERR) {
  174.                                         /* if want to unload paper */
  175.                                         if (!(x & SPECIAL_NOFORMFEED)) {
  176.                                                 /* eject paper */
  177.                                                 err = (*(PD->pd_PWrite))
  178.                                                         ("\014", 1);
  179.                                         }
  180.                                 }
  181.                         }
  182.                         /*
  183.                                 flag that there is no alpha data waiting that
  184.                                 needs a formfeed (since we just did one)
  185.                         */
  186.                         PED->ped_PrintMode = 0;
  187.                          /* wait for both buffers to empty */
  188.                         (*(PD->pd_PBothReady))();
  189.                         for (i=0; i<NumColorBufs; i++) {
  190.                             if (colors[i] != NULL) 
  191.                                 FreeMem(colors[i], BufSize);
  192.                         }
  193.                         break;
  194.  
  195.                 case 5 : /* Pre-Master Initialization */
  196.                         /*
  197.                                 ct      - 0 or pointer to IODRPReq structure.
  198.                                 x       - io_Special flag from IODRPReq struct
  199.                                 y       - 0.
  200.                         */
  201.                         /* select density */
  202.                         SetDensity(x & SPECIAL_DENSITYMASK);
  203.                         break;
  204.         }
  205.         return(err);
  206. }
  207. /* New dump routines which replaces older CompactBuf routine 8/4/02 */
  208.  
  209. int DumpBWPrint(UBYTE *colors[],ULONG BufSize)
  210. {
  211.     /* Dump B&W grpahic data */
  212.     
  213.     ULONG size;
  214.     UBYTE *ptr;
  215.     char cmd[10] = "\033*b0m000W";
  216.     int err, method, mem, i=0;
  217.     
  218.     /* Remove trailing white space */
  219.     mem = FALSE;
  220.     size = StripWhiteSpace(colors[0], BufSize);
  221.     if (size>0) {     
  222.         /* Compress data */
  223.         if (ptr = AllocMem(BufSize, MEMF_PUBLIC|MEMF_CLEAR)) {
  224.             i = CompressMethod2(colors[0], ptr, size);
  225.             mem = TRUE;
  226.             if (i<0) {
  227.                 method = 0;
  228.                 i = size;
  229.             } else
  230.                 method = 2;
  231.         }   
  232.         else {
  233.             ptr = colors[0];
  234.             method = 0;
  235.             mem = FALSE;
  236.         }
  237.         
  238.         cmd[3] = method | '0';
  239.         cmd[5] = (i/100) | '0';
  240.         cmd[6] = (i - (i/100)*100) / 10 | '0';
  241.         cmd[7] = i % 10 | '0';
  242.     } else {
  243.         method = 0;
  244.         i = 0;
  245.         cmd[3] = method | '0';
  246.         cmd[5] = '0';
  247.         cmd[6] = '0';
  248.         cmd[7] = '0';
  249.         ptr=colors[0];
  250.     }
  251.     
  252.     (*(PD->pd_PWrite))(cmd,9);
  253.     err = (*(PD->pd_PWrite))(ptr, i);
  254.     if (mem) FreeMem(ptr, BufSize);
  255.     return err;
  256. }
  257.  
  258. int DumpColorPrint(UBYTE *colors[],ULONG BufSize)
  259. {
  260.     /* Dump Color graphic data */
  261.     
  262.     ULONG size;
  263.     UBYTE *ptr;
  264.     char startcmd[6] = "\033*b0W";
  265.     char colorcmd[10] = "\033*b0m000V";
  266.     int err, ct, method, mem, i=0;
  267.      
  268.     err = (*(PD->pd_PWrite))(startcmd,5);
  269.     mem = FALSE;
  270.     
  271.     for (ct=0; ct<4 && err == PDERR_NOERR; ct++) {
  272.         
  273.         /* Strip trailing White Space */
  274.         size = StripWhiteSpace(colors[ct], BufSize);
  275.         if (size>0) {
  276.             /* Compress data */
  277.             if (ptr = AllocMem(BufSize, MEMF_PUBLIC|MEMF_CLEAR)) {
  278.                  i = CompressMethod2(colors[ct], ptr, size);
  279.                  mem = TRUE;
  280.                  if (i < 0) {
  281.                     method = 0;
  282.                     i = size;
  283.                  } else
  284.                     method = 2; 
  285.             }
  286.             else {
  287.                  ptr = colors[ct];
  288.                  method = 0;
  289.                  mem = FALSE;
  290.             }
  291.    
  292.             colorcmd[3] = method | '0';
  293.             colorcmd[5] = (i/100) | '0';
  294.             colorcmd[6] = (i - (i/100)*100) / 10 | '0';
  295.             colorcmd[7] = i % 10 | '0';
  296.         } else {
  297.             method = 0;
  298.             i = 0;
  299.             colorcmd[3] = method | '0';
  300.             colorcmd[5] = '0';
  301.             colorcmd[6] = '0';
  302.             colorcmd[7] = '0';
  303.             ptr = colors[ct];
  304.         }  
  305.         (*(PD->pd_PWrite))(colorcmd,9);
  306.         err = (*(PD->pd_PWrite))(ptr, i);
  307.         if (mem) FreeMem(ptr, BufSize);
  308.     }
  309.    return err;
  310. }
  311.  
  312. /* This routine makes a local copy of the colour data and applies
  313.  * gamma correction to it.
  314.  */
  315. VOID CorrectColours(UBYTE *gamma_table, UBYTE *colors[], ULONG width)
  316. {
  317.     LONG x,c;
  318.         UBYTE *ptrstart;
  319.  
  320.         for (c=0; c<=3; c++) {  /* Repeat for CMYK colours */
  321.            ptrstart = colors[c];
  322.            
  323.        for(x = 0 ; x < width ; x++) /* for each byte */
  324.        {
  325.         *(ptrstart++) = gamma_table[*ptrstart];
  326.            }
  327.         }
  328. }
  329.  
  330.